﻿// basetool_demo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "SLT_tool_pub.h"
#include "SL_asynFramePub.h"

#define  _message_id_test_startnotify    1
#define  _message_id_test_msg    2

class clientSession : public SLF_SessionBase
{
public:
    virtual void OnStart() {
        SLT_LOG_ERR("clientSession OnStart---");
    }
    virtual void OnStop() {
    }
    virtual void OnMsgArrive(SLF_senderPtr& _sender, uint32_t srcSid, int msgid, SLF_sendMemBasePtr& _data) {
        SLT_LOG_ERR("OnMsgArrive localid:%d,srcid:%d,msgid:%d", getSessID(), srcSid, msgid);
        switch (msgid) {
            SLF_MSGARRIVE(_message_id_test_startnotify, OnStartNotify);
            SLF_MSGARRIVE(_message_id_test_msg, OnTestMsg);
        }
    }
    virtual void OnTimer(uint32_t timerID, int _intervalMs, bool periodic) {
    }

    void OnStartNotify(SLF_senderPtr& _sender, uint32_t srcSid, int msgid, SLF_sendMemBasePtr& _data)
    {
        SLF_sendMemBasePtr data(new SLF_sendMemBase);
        std::stringstream str;
        str << "hello dst:" << m_dst_id;
        data->add(str.str().c_str(), str.str().length() + 1);

        SLT_LOG_ERR("SLF_postMsg _message_id_test_msg,local:%d,to id:%d", getSessID(), m_dst_id);
        SLF_postMsg(m_dst_id, _message_id_test_msg,getSessID(), data);
    }
    void OnTestMsg(SLF_senderPtr& _sender, uint32_t srcSid, int msgid, SLF_sendMemBasePtr& _data)
    {
        SLT_LOG_ERR("recv msg(_message_id_test_msg),%s", _data->getData());
        //if (0 == m_dst_id) {
        //    //可以根据条件座应答处理
        //    SLF_sendMemBasePtr data(new SLF_sendMemBase);
        //    std::stringstream str;
        //    str << "ack hello " << m_dst_id;
        //    data->add(str.str().c_str(), str.str().length() + 1);

        //    SLT_LOG_ERR("SLF_postMsg _message_id_test_msg ack");
        //    SLF_postMsg(srcSid, _message_id_test_msg, getSessID(), data);
        //}
    }

    std::string m_name;
    int         m_dst_id = 0;
};
typedef std::shared_ptr< clientSession>  clientSessionSptr;
int main()
{
    SLT_logInit();
    SLF_init(1);
    clientSessionSptr clinetSptr = std::make_shared<clientSession>();
    clinetSptr->m_name = "client-1";
    SLF_addSession(clinetSptr);

    clientSessionSptr clinetSptr2 = std::make_shared<clientSession>();
    clinetSptr2->m_name = "client-2";
    clinetSptr2->m_dst_id = clinetSptr->getSessID();
    SLF_addSession(clinetSptr2);

    SLF_sendMemBasePtr sendmem;
    SLF_postMsg(clinetSptr2->getSessID(), _message_id_test_startnotify, 0, sendmem);

    //sl_createTcpServer
    printf("press enter exit\n");
    getchar();
}

